package module.view
{
	import commons.PipeAwareModuleConstants;
	
	import module.ModuleConstants;
	
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.utilities.pipes.interfaces.IPipeMessage;
	import org.puremvc.as3.multicore.utilities.pipes.messages.Message;
	import org.puremvc.as3.multicore.utilities.pipes.plumbing.Junction;
	import org.puremvc.as3.multicore.utilities.pipes.plumbing.JunctionMediator;

	public class ModuleJunctionMediator extends JunctionMediator
	{
		public static const NAME:String = "ModuleJunctionMediator";
		
		public function ModuleJunctionMediator()
		{
			trace("ModuleJunctionMediator Constructor");
			super(NAME, new Junction());
			
		}
		
		public function checkPipes():void{
			trace(junction.hasPipe(PipeAwareModuleConstants.MODULE_TO_SHELL_PIPE));
			trace(junction.hasPipe(PipeAwareModuleConstants.SHELL_TO_MODULE_PIPE));
		}
		
		override public function onRegister():void{
			
		}
		
		override public function onRemove():void{
			 junction.removePipe( PipeAwareModuleConstants.MODULE_TO_SHELL_PIPE );
			 junction.removePipe( PipeAwareModuleConstants.SHELL_TO_MODULE_PIPE );
		}
		
		override public function listNotificationInterests():Array{
			return [ModuleConstants.CHECK_PIPES,
					JunctionMediator.ACCEPT_INPUT_PIPE,
					JunctionMediator.ACCEPT_OUTPUT_PIPE,
					PipeAwareModuleConstants.SEND_MESSAGE_SHELL];
		}
		
		override public function handleNotification(notification:INotification):void{
			switch(notification.getName()){
				case ModuleConstants.CHECK_PIPES: 
					this.checkPipes();
				break;
				case PipeAwareModuleConstants.SEND_MESSAGE_SHELL: 
					junction.sendMessage(PipeAwareModuleConstants.MODULE_TO_SHELL_PIPE, new Message(PipeAwareModuleConstants.MODULE_TO_SHELL_MESSAGE, PipeAwareModuleConstants.SEND_MESSAGE_SHELL));
				break;
				default: super.handleNotification(notification); break;
			}
		}
		
		override public function handlePipeMessage(message:IPipeMessage):void{
			var header:String = Message(message).getHeader() as String;
			switch(header){
				case PipeAwareModuleConstants.SEND_MESSAGE_MODULE: 
					sendNotification(PipeAwareModuleConstants.SEND_MESSAGE_MODULE);
				break;
				case PipeAwareModuleConstants.REMOVE_MODULE: 
					sendNotification(PipeAwareModuleConstants.REMOVE_MODULE);
				break;
				default: break;
			}
		}
		
	}
}